package elsepackage.temple.io;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.feilong.commons.core.io.IOUtil;

public class Util{

	public static boolean copyFile(String fileName,String newFileName){
		BufferedOutputStream bos = null;
		FileInputStream in = null;
		File old = new File(fileName);
		if (!old.exists()){
			return false;
		}
		File newPath = new File(newFileName);
		if (!newPath.getParentFile().exists()){
			newPath.getParentFile().mkdirs();
		}
		try{
			in = new FileInputStream(fileName);
			bos = new BufferedOutputStream(new FileOutputStream(newFileName));
			byte[] buf = new byte[1024];
			int b = in.read(buf, 0, buf.length);
			while (b != -1){
				bos.write(buf, 0, b);
				b = in.read(buf);
			}
		}catch (FileNotFoundException e){
			e.printStackTrace();
			return false;
		}catch (IOException e1){
			e1.printStackTrace();
			return false;
		}finally{
			try{
				in.close();
				bos.close();
			}catch (IOException e){
				e.printStackTrace();
			}
		}
		return true;
	}

	/**
	 * 执行命令
	 * 
	 * @param cmd
	 * @return
	 */
	public static int executeCommand(String cmd){
		int ret = 0;
		try{
			String line;
			Process process = Runtime.getRuntime().exec(cmd);
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
			while ((line = bufferedReader.readLine()) != null){
				System.out.println(line);
			}
			process.waitFor();
			ret = process.exitValue();
		}catch (Exception e){
			e.printStackTrace();
			ret = -1;
		}
		return ret;
	}

	/** 显示当前系统物理状况 */
	public static void showOSState(){
		long maxMemory = Runtime.getRuntime().maxMemory() / 1024;//Java 虚拟机试图使用的最大内存量
		long totalMemory = Runtime.getRuntime().totalMemory() / 1024;//Java 虚拟机中的内存总量。
		long freeMemory = Runtime.getRuntime().freeMemory() / 1024;//Java 虚拟机中的空闲内存量
		long usedMemory = totalMemory - freeMemory;
		freeMemory = maxMemory - usedMemory;
		System.out.println("OS State: mem used:" + "" + "KB,mem free:" + freeMemory + "KB,mem total:"
				+ IOUtil.convertFileSize(Runtime.getRuntime().maxMemory()));
		System.out.println("OS State: mem used:" + usedMemory + "KB,mem free:" + freeMemory + "KB,mem total:" + totalMemory + "KB.");
	}

	/** 显示 程序运行的 系统环境 信息 */
	public static void printEnvironmentInfo(){
		try{
			Properties props = System.getProperties();
			StringBuffer sb = new StringBuffer();
			sb.append("\n----------------------Environment Info--------------------------\n");
			sb.append("os.name : " + props.getProperty("os.name") + "\n"); //操作系统名称
			sb.append("os.arch : " + props.getProperty("os.arch") + "\n"); //操作系统构架
			sb.append("os.version : " + props.getProperty("os.version") + "\n"); //操作系统版本
			sb.append("java.version : " + props.getProperty("java.version") + "\n"); //Java 运行时环境版本
			sb.append("java.vendor : " + props.getProperty("java.vendor") + "\n"); //Java 运行时环境供应商
			sb.append("java.vm.name : " + props.getProperty("java.vm.name") + "\n"); //Java 虚拟机实现名称
			sb.append("java.home : " + props.getProperty("java.home") + "\n"); //Java 安装目录
			sb.append("java.class.path : " + props.getProperty("java.class.path") + "\n"); //Java 类路径
			sb.append("java.library.path : " + props.getProperty("java.library.path") + "\n"); //加载库时搜索的路径列表   
			sb.append("user.name : " + props.getProperty("user.name") + "\n"); //用户的账户名称
			sb.append("user.home : " + props.getProperty("user.home") + "\n"); //用户的主目录
			sb.append("user.dir : " + props.getProperty("user.dir") + "\n"); //用户的当前工作目录
			sb.append("file.encoding : " + props.getProperty("file.encoding") + "\n");
			sb.append("--------Disk information---------\n");
			printDiskInfo(sb);
			sb.append("----------------------------------------------------------------");
			System.out.println(sb.toString());
		}catch (Exception e){}
	}

	/** 记录磁盘信息 */
	public static void printDiskInfo(StringBuffer sb){
		try{
			File[] roots = File.listRoots();
			for (File _file : roots){
				sb.append(_file.getPath() + "\n");
				sb.append(_file.getName() + "\n");
				sb.append("Free space = " + IOUtil.convertFileSize(_file.getFreeSpace()) + "\n");
				sb.append("Usable space = " + IOUtil.convertFileSize(_file.getUsableSpace()) + "\n");
				sb.append("Total space = " + IOUtil.convertFileSize(_file.getTotalSpace()) + "\n");
				sb.append("\n");
			}
		}catch (Exception e){}
	}

	/** 转换 byte数组 为 char数组 */
	public static char[] bytesToChars(byte[] bytes){
		String s = new String(bytes);
		char[] c = s.toCharArray();
		return c;
	}

	/**
	 * 从一段文字中提取IP地址
	 * 
	 * @param str
	 * @return
	 */
	public static String getIP(String str){
		Pattern p = Pattern.compile("\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}");
		Matcher m = p.matcher(str);
		boolean b = m.find();
		String ip = "";
		if (b){
			ip = m.group();
			return ip;
		}
		return null;
	}
}
